﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Store
{
    public partial class 选择销售合同 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.DingHuo.DataBind();
                this.ShouHuo.DataBind();
                this.Button_Query_Click(null, null);
            }
            else
            {
                this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
            }
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        private string GetState()
        {
            string state = " and ht.[是否完成]=0";

            if (this.DanHao.Text != "")
            {
                state += " and ht.[合同编号] like '%" + this.DanHao.Text.Replace("'","") + "%'";
            }

            if (this.DingHuo.Text != "0")
            {
                state += " and ht.[订货单位ID]=" + this.DingHuo.Text;
            }
            if (this.ShouHuo.Text != "0")
            {
                state += " and ht.[收货单位ID]=" + this.ShouHuo.Text;
            }

            //如果什么都没有选择，则默认为1
            if (this.ChanPin.Text != "")
            {
                state += " and ht.[产品ID]=" + this.ChanPin.Text;
            }
            else
            {
                //如果什么都没有选择，则默认为1
                state += " and ht.[产品ID]=1 ";
            }

            DateTime d;
            if (DateTime.TryParse(this.Date1.Text, out d))
            {
                state += " and ht.日期>='" + d.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out d))
            {
                state += " and ht.日期<='" + d.ToShortDateString() + "'";
            }

            if (state.StartsWith(" and"))
            {
                state = state.Substring(4);
            }
            if (state != "")
            {
                state = " where " + state;
            }
            return state;
        }

        protected void Button_Query_Click(object sender, EventArgs e)
        {
            this.SqlDataSource1.SelectCommand = "";
            this.SqlDataSource1.SelectCommand =
@"SELECT ht.ID
,ht.合同编号
,d1.名称 AS 订货单位
,d2.名称 AS 收货单位
,cp.[品名]+' '+cp.[钢级]+' '+cp.[规格] AS 产品
,ht.数量 AS 订货量
,(SELECT SUM(总重量) AS 出货量 FROM 产品出库单 WHERE 合同ID=ht.ID) AS 发货量
,ht.日期	
FROM 产品销售合同  AS ht
left outer join 产品订货单位 d1 ON ht.订货单位ID = d1.ID
left outer join 产品订货单位 d2 ON ht.收货单位ID = d2.ID
inner join 产品 as cp ON ht.产品ID=cp.ID " + GetState() + "ORDER BY ID DESC";

            this.ViewState["SelectCommand"] = this.SqlDataSource1.SelectCommand;
            this.Button_NextStep.Enabled = false;
            this.GridView1.SelectedIndex = -1;
        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Button_NextStep.Enabled = true;
            //this.Button_NextStep.OnClientClick = "parent.swin2_load('/Store/ChanPin_Out.aspx?HTID="
            //    + this.GridView1.SelectedDataKey[0].ToString() + "',1000,650,'yes');return false;";
            
        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }

        protected void Button_Reset_Click(object sender, EventArgs e)
        {
            this.ChanPin.Text = "0";
            this.DingHuo.Text = "0";
            this.DanHao.Text = "";
            this.Date1.Text = "";
            this.Date2.Text = "";
        }

        protected void Button_NextStep_Click(object sender, EventArgs e)
        {
            Response.Redirect("产品出库单录入.aspx?HTID=" + this.GridView1.SelectedDataKey[0]);
        }

    }
}